﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.OA
{
    public partial class OAService : IOAService
    {
        public ServiceResult SaveArticle(Article article)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.Save(article, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public Article GetArticle(Guid Id)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.Set<Article>().Where(p =>p.Id == Id).SingleOrDefault();
            }
        }

        public PagedList<Article> GetArticles(string typeCode, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<Article> list = context.Set<Article>().Where(p => p.TypeCode == typeCode)
                    .OrderBy(p => p.CreatedDate)
                    .Skip(start)
                    .Take(limit).ToList();
                return new PagedList<Article>(list, start, limit, list.Count);
            }
        }
    }
}
